package com.geng.invoker;

import com.geng.dto.MyRequest;
import com.geng.dto.MyResponse;

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class SendRequst {

    public static MyResponse response;

/**
     *   调用者线程数*/


    private static int threadWorkerNumber = 10;
/*

*
     * 线程池去提交任务
     */

    public static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(threadWorkerNumber);



    public void deSendRequst(MyRequest request) {
        try {
            //构建用来发起调用的线程池
            //根据服务提供者的ip,port,构建InetSocketAddress对象,标识服务提供者地址
            String serverIp = request.getProviderService().getServerIp();
            int serverPort = request.getProviderService().getServerPort();
            InetSocketAddress inetSocketAddress = new InetSocketAddress(serverIp, serverPort);
            //提交本次调用信息到线程池fixedThreadPool,发起调用
            Future<MyResponse> responseFuture = fixedThreadPool.submit(SendRequestThread.of(inetSocketAddress, request));
            //获取调用的返回结果
            SendRequst.response = responseFuture.get();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
